using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Goods.Dto;
using JTZY.Model.Goods;
using JTZY.Repository;
using JTZY.Service.Goods.IGoodsService;
using System.Collections.Generic;

namespace JTZY.Service.Goods
{
    /// <summary>
    /// 商品分类Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmsgoodscategoryService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmsgoodscategoryService : BaseService<Corecmsgoodscategory>, ICorecmsgoodscategoryService
    {
        /// <summary>
        /// 查询商品分类列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsgoodscategoryDto> GetList(CorecmsgoodscategoryQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<Corecmsgoodscategory, CorecmsgoodscategoryDto>(parm);

            return response;
        }

        /// <summary>
        /// 查询商品分类树列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public List<Corecmsgoodscategory> GetTreeList(CorecmsgoodscategoryQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsgoodscategory>();


            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToTree(it => it.Children, it => it.ParentId, 0);

            return response;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Corecmsgoodscategory GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加商品分类
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmsgoodscategory AddCorecmsgoodscategory(Corecmsgoodscategory model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改商品分类
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCorecmsgoodscategory(Corecmsgoodscategory model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmsgoodscategory> QueryExp(CorecmsgoodscategoryQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsgoodscategory>();

            return predicate;
        }
    }
}